package com.duoec.magichour.controller.api;

import com.duoec.magichour.core.annotation.Access;
import com.duoec.magichour.core.exceptions.BusinessException;
import com.duoec.magichour.core.utils.MobileUtils;
import com.duoec.magichour.core.utils.PasswordUtils;
import com.duoec.magichour.core.web.BaseApiController;
import com.duoec.magichour.dto.BaseResponse;
import com.duoec.magichour.dto.request.UserLoginWithCodeDto;
import com.duoec.magichour.dto.request.UserLoginWithPwdDto;
import com.duoec.magichour.dto.request.UserRegistSave;
import com.duoec.magichour.dto.response.UserDetailDto;
import com.duoec.magichour.dto.response.UserSimpleInfoDto;
import com.duoec.magichour.pojo.Authorize;
import com.duoec.magichour.service.CaptchaService;
import com.duoec.magichour.service.UserService;
import com.google.common.base.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 用户接口
 */
@RestController
@RequestMapping("/api/user")
public class UserApiController extends BaseApiController {
    @Autowired
    private UserService userService;

    @Autowired
    private CaptchaService captchaService;

    /**
     * 获取用户详情信息
     *
     * @param userId 用户ID
     * @return
     */
    @RequestMapping("/{userId:\\d+}")
    public BaseResponse<UserDetailDto> detail(@PathVariable Long userId) {
        UserDetailDto userDetail = userService.getDetail(getUserInfo(), userId);
        return BaseResponse.success(userDetail);
    }

    /**
     * 用户注册
     *
     * @param request 注册信息
     * @return
     */
    @RequestMapping(method = RequestMethod.POST)
    public BaseResponse<UserSimpleInfoDto> regist(@RequestBody UserRegistSave request) {
        //参数验证
        //检查是否有效手机号码
        String mobile = request.getMobile();
        if (!MobileUtils.isAvailabeMobile(mobile)) {
            throw new BusinessException(BaseResponse.CODE_PARAM_INVALID, "无效手机号码！");
        }

        //检查验证码
        if (!captchaService.validate(request.getCaptcha())) {
            throw new BusinessException(BaseResponse.CODE_PARAM_INVALID, "手机验证码错误！");
        }

        //检查密码
        if (!PasswordUtils.isAvailablePassword(request.getPassword())) {
            throw new BusinessException(BaseResponse.CODE_PARAM_INVALID, "密码太简单！");
        }

        UserSimpleInfoDto user = userService.regist(request);
        return BaseResponse.success(user);
    }

    /**
     * 使用手机验证码登录
     *
     * @param request 登录信息
     * @return
     */
    @RequestMapping(method = RequestMethod.POST, value = "/login-code")
    public BaseResponse<Authorize> login(@RequestBody UserLoginWithCodeDto request) {
        //参数验证
        //检查是否有效手机号码
        String mobile = request.getMobile();
        if (!MobileUtils.isAvailabeMobile(mobile)) {
            throw new BusinessException(BaseResponse.CODE_PARAM_INVALID, "无效手机号码！");
        }

        //检查密码
        if (Strings.isNullOrEmpty(request.getCode()) || request.getCode().length() < 4) {
            throw new BusinessException(BaseResponse.CODE_PARAM_INVALID, "手机验证码错误！");
        }

        Authorize authorize = userService.loginWithCode(request);
        return BaseResponse.success(authorize);
    }

    /**
     * 使用密码登录
     *
     * @param request 登录信息
     * @return
     */
    @RequestMapping(method = RequestMethod.POST, value = "/login-pwd")
    public BaseResponse<Authorize> login(@RequestBody UserLoginWithPwdDto request) {
        //参数验证
        //检查是否有效手机号码
        String mobile = request.getMobile();
        if (!MobileUtils.isAvailabeMobile(mobile)) {
            throw new BusinessException(BaseResponse.CODE_PARAM_INVALID, "无效手机号码！");
        }

        //检查密码
        if (!PasswordUtils.isAvailablePassword(request.getPassword())) {
            throw new BusinessException(BaseResponse.CODE_PARAM_INVALID, "密码太简单！");
        }

        Authorize authorize = userService.loginWithPwd(request);
        return BaseResponse.success(authorize);
    }

    /**
     * 退出登录
     *
     * @return
     */
    @Access
    @RequestMapping(method = RequestMethod.DELETE, value = "/logout")
    public BaseResponse<Boolean> logout() {
        userService.logout(getUserInfo());
        return BaseResponse.success(true);
    }
}
